/** 
* Projet AP5 - Agenda PDA
* AgendaControl.java
*/

package pda.control;

import pda.view.*;
import pda.datas.*;

import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;

import java.util.ArrayList;
import java.util.Arrays;

/** Classe qui permet le demarrage de l'application PDA */

public class AgendaControl extends MouseAdapter implements ActionListener, IApplication  {

	
//##########################################################################################################
//										Declaration des variables
//##########################################################################################################

	/** the name of the application */
	protected String name = "Agenda";
	/** the view of the application */
	protected AgendaView view;
	/** the engine of the application */
	protected AgendaEngine engine;
	/** the date for the application */
	protected DateDisplayed datePerso;
	/** the activity panel for the application */
	protected ActivityPanel actPanel;
	/** the panel where the error or displayed in the application */
	protected PanelError error;

//##########################################################################################################
//										Constructeur
//##########################################################################################################
	/**
	*  The Agenda constructor
	*/
	public AgendaControl() {
	    //data
	    datePerso = new DateDisplayed() ;
	    engine = new AgendaEngine();
	    //ihm
	    view = new AgendaView(this);
	    actPanel = new ActivityPanel(this, view);
	    error = new PanelError ( view, actPanel);
						
	    engine.charger(null);
	} 

//##########################################################################################################
//										Methodes
//##########################################################################################################
	/**
	* See documentation interface
	* @param e the event of clicking on Component
	*/	
	public void actionPerformed(ActionEvent e) {
	
		String evt = e.getActionCommand ();
		
		if (evt.equals ("home")) {
			view.getMenu().setVisible (true); 
			int posXpopup = 0; 
			int posYpopup = - (view.getMenu().getHeight ()); 
			view.getMenu().show (view.getOptions(),posXpopup, posYpopup); 
		}else if (evt.equals("prevMonth")){
			view.setDateChoosenLbl(datePerso.changeSubMonth(view.getDateChoosenLbl()));
			view.initHome();
		}else if(evt.equals("nextMonth")){
			view.setDateChoosenLbl(datePerso.changeAddMonth(view.getDateChoosenLbl()));
			view.initHome();
		}else if (evt.equals("addCourse")){
			actPanel.setPanelCourse();
		}else if(evt.equals("addRdv")){
			actPanel.setPanelRdv();
		}else if(evt.equals("addMeeting")){
			actPanel.setPanelMeeting();
		}else if(evt.equals("addActivity")){
			actPanel.setPanelActivity();
		}else if(evt.equals("prevDay")){
			actPanel.setDateChoosen(datePerso.changeSubDay(view.getDateChoosenLbl()));
			view.setDateChoosenLbl(datePerso.changeSubDay(view.getDateChoosenLbl()));
			view.refresh();
		}else if(evt.equals("nextDay")){
			actPanel.setDateChoosen(datePerso.changeAddDay(view.getDateChoosenLbl()));
			view.setDateChoosenLbl(datePerso.changeAddDay(view.getDateChoosenLbl()));
			view.refresh();
		}else if(evt.equals("searchChange")){
			((DefaultListModel) view.getListeActiviteJour().getModel()).clear();
			if ( engine.getEvt(view.getSearchDate()).size() != 0 ){
				for ( String tmp :  engine.getEvt(view.getSearchDate() ) )
					((DefaultListModel) view.getListeActiviteJour().getModel()).addElement(tmp);
		
			}
		}else if(evt.equals("search")){
			view.search();
		}else if(evt.equals("retour")){
			view.initHome();
			
		}else if(evt.equals("validRDV")){
		String motifErreur = engine.addActivite(

			view.getDateChoosen(),
			actPanel.getMotif().getText(),
			actPanel.getHeureD().getText(),
			actPanel.getMinuteD().getText(),
			actPanel.getHeureF().getText(),
			actPanel.getMinuteF().getText(),
			actPanel.getRepetition(),
			actPanel.getLieu().getText(),
			actPanel.getListe().getText());

		if ( motifErreur.equals("ok" )){
			view.initHome() ;
			view.refresh();
		}else{
			error.disp(motifErreur);
			view.refresh();
		}
		}else if(evt.equals("validReunion")){
		String motifErreur = engine.addActivite(

			view.getDateChoosen(),
			actPanel.getMotif().getText(),
			actPanel.getHeureD().getText(),
			actPanel.getMinuteD().getText(),
			actPanel.getHeureF().getText(),
			actPanel.getMinuteF().getText(),
			actPanel.getRepetition(),
			actPanel.getLieu().getText(),
			actPanel.getInitiateur().getText(),
			actPanel.getListe().getText(),
			actPanel.getMail().getText(),
			actPanel.getDuree().getText());

		if ( motifErreur.equals("ok" )){
					view.initHome() ;
		}else{
			error.disp(motifErreur);
			view.refresh();
			}
		}else if(evt.equals("validCours")){
			String motifErreur = engine.addActivite(

			view.getDateChoosen(),
			actPanel.getMotif().getText(),
			actPanel.getHeureD().getText(),
			actPanel.getMinuteD().getText(),
			actPanel.getHeureF().getText(),
			actPanel.getMinuteF().getText(),
			actPanel.getRepetition(),
			actPanel.getLieu().getText(),
			actPanel.getProf().getText(),
			actPanel.getMatiere().getText());

		if (motifErreur.equals("ok" )){
					view.initHome() ;
		}else{
			error.disp(motifErreur);
			view.refresh();
		}
		}else if(evt.equals("validActivite")){
		String motifErreur = engine.addActivite(

			view.getDateChoosen(),
			actPanel.getMotif().getText(),
			actPanel.getHeureD().getText(),
			actPanel.getMinuteD().getText(),
			actPanel.getHeureF().getText(),
			actPanel.getMinuteF().getText(),
			actPanel.getRepetition());

		if ( motifErreur.equals("ok" )){
					view.initHome() ;
		}else{
			error.disp(motifErreur);
			view.refresh();
		}
		
		}else if(evt.equals("load")) {
			view.LoadOrSave("Charger");
		
		}else if(evt.equals("save")) {		
			view.LoadOrSave("Sauver");
		
		}else if(evt.equals("Charger")) {
		
			String tmpp = (view.getSaveLoadFile().length() != 0 ) ? view.getSaveLoadFile() :  null ;
			engine.charger(tmpp);
			view.initHome();
		}else if(evt.equals("Sauver")) {
			String tmpp = (view.getSaveLoadFile().length() != 0 ) ? view.getSaveLoadFile() :  null ;
			engine.sauver(tmpp);
			view.initHome();
		}else if(evt.equals("deleteAll")) {
			engine.deleteAllEvt();
			view.initHome();
		}else if(evt.equals("deletePrevious")) {
			engine.deletePreviousEvt(view.getToday());
			view.initHome();
		}else if(evt.equals("deleteOne")) {
			engine.deleteEvt(view.getSelectAct(), view.getDateChoosen());
			view.initHome();
		}else if(evt.equals("details")) {
			actPanel.setPanelDetails();
		}else{
			System.out.println("mettre un set action commande et l'inplementer dans agendaControl");
		}
		
	}
	
	/**
	*see documentation interface
	*@param e the event of clicking on Component
	*/
	public void mouseClicked ( MouseEvent e ) {
	
		if (e.getSource() instanceof JList){
			view.getNameFile().setText("");
			try {
				int index = view.getNameList().locationToIndex(e.getPoint());
				DefaultListModel tab = (DefaultListModel)( view.getNameList().getModel());
				String cle = (String)tab.get(index);
				view.setFile(cle);
			}catch (ArrayIndexOutOfBoundsException exc){

			}
		}
		if (e.getSource() instanceof JTextField){
			((DefaultListSelectionModel)view.getNameList().getSelectionModel()).clearSelection();
		}
		
	}
	
	/**
	* Return the boolean which show the existence or not
	*@param date the date where we search for event
	*@return true if an event exist on this date
	*/
	public boolean getEvtExiste(String date){ return engine.getEvtExiste(date);}
	
	/**
	* Return the ArrayList of all event for a date
	*@param date a date where we want the event for
	*@return an ArrayList of all event for the day gives in param
	*/
	public ArrayList<String> getEvt(String date){ return engine.getEvt(date); }

	/**
	* Retourne l'objet AgendaEngine
	* @return engine 
	*/
	public AgendaEngine getEngine(){ return engine; }

//##########################################################################################################
//										Methodes natives (non modifier)
//##########################################################################################################


    /**
    * Demarre l'application
    */	
    public void start(PdaCtrl pda) {
        view.launchAgendaView();
    } 
	
    /**
    * Ferme l'application
    */		
    public boolean close() {
		return true;
    } 

    /**
    * Retoune le nom de l'application
    * @return name
    */
    public String getAppliName() {
		return name;
    } 
	
    /**
    * Retoune le panel de l'application
    * @return view
    */	
    public JPanel getAppliPanel() {
		return view.getPanel();
    }
	


    /**
    * Modifie le nom de l'application
    */	
    public void setAppliName ( String theName ) {
	this.name = theName;
    } // ---------------------------------------------------------- setAppliName()

} 
